﻿// ===============================================================================
// DotNet.Platform 开发框架 2016 版权所有
// ===============================================================================

using System;
using System.Web.Mvc;
using DotNet.Core.Entity;
using DotNet.Core.Service;
using DotNet.Extensions;
using DotNet.Helper;
using DotNet.Utility;
using DotNet.Web.Areas.AuthWeb.Controllers;

namespace DotNet.Web.Areas.WXCWeb.Controllers
{
    public class LinkSystemController : AuthController
    {
        private LinkSystemService service = new LinkSystemService();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Grid(string name)
        {
            var list = service.GetPageList(PageInfo(), name);
            return View(list);
        }

        public ActionResult Create()
        {
            MarkCreate();
            return EditCore(new LinkSystem
            {
                Id = StringHelper.Guid()
            });
        }

        public ActionResult Edit(string id)
        {
            var entity = service.GetById(id);
            if (entity == null) return NotFound(id);
            return EditCore(entity);
        }

        private ActionResult EditCore(LinkSystem entity)
        {
            return View("Edit", entity);
        }

        [HttpPost]
        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        public ActionResult Save(LinkSystem entity)
        {
            var result = service.Save(entity, IsCreate);
            return Json(result);
        }

        [HttpPost]
        public ActionResult Delete(string id)
        {
            service.Delete(id.SplitToArray());
            return Json(BoolMessage.True);
        }
        
        public ActionResult Details(string id)
        {
            var entity = service.GetById(id);
            if (entity == null) return NotFound(id);
            return View(entity);
        }

        public ActionResult Export(string name)
        {
            return Export(service.GetList(name));
        }

        private ActionResult NotFound(string id)
        {
            return NotFound("获取 链接系统 错误", $"无法找到 Id = {id} 的链接系统");
        }
    }
}
